//
//  YYT_HIstoryViewController.m
//  AudioVisualFeast
//
//  Created by 张琪 on 16/2/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "QL_HIstoryViewController.h"
#import "QL_DataBaseSingleton.h"
#import "QL_HistoryTableViewCell.h"

@interface QL_HIstoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *tableView;
@end

@implementation QL_HIstoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"播放记录";
    //    关闭半透明效果
    self.navigationController.navigationBar.translucent = NO;
    //    设置样式
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    //    导航栏元素样式
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //    首页左侧抽屉图片点击方法
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"MVfanhui"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(left2:)];
    //    首页右侧搜索图片点击方法
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清除观看记录" style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    //    导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    //    数据库
    QL_DataBaseSingleton *dataBaseHandle = [QL_DataBaseSingleton shareDataBase];
    //    创建观看历史表
    [dataBaseHandle createSeeTable];
    //    查询之后 用数组接收
    self.history = [[NSArray alloc] initWithArray:[dataBaseHandle selectSeeDataBasearray]];
    //    放TableView
    [self.view addSubview:self.tableView];
    //    刷新
    [_tableView reloadData];
    
    
    
}

// 右侧清除全部观看记录
- (void)right:(UIButton *)sender {
    // 删除表
    [[QL_DataBaseSingleton shareDataBase]deleteSeeTable];
    [self.tableView reloadData];

    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"缓存已清除" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self.tableView reloadData];
        [[QL_DataBaseSingleton shareDataBase]createSeeTable];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];

        
    }];
    [alertC addAction:action];
 
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];

}

// 导航栏左侧按钮点击方法
- (void)left2:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

// 懒加载TableView
- (UITableView *)tableView {
   
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[QL_HistoryTableViewCell class] forCellReuseIdentifier:@"cell"];
        

    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 返回图片的个数
    NSArray *array = [NSArray arrayWithArray:self.history[2]];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QL_HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    // 0 id
    // 1 headtitle
    // 2 pic
    NSArray *array = [[NSMutableArray alloc] initWithObjects:self.history[2][indexPath.row],self.history[1][indexPath.row],nil];
    cell.DataSource = array;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
